package com.example.service.impl.contractManage;

import com.example.entity.Contract;
import com.example.entity.ContractVO;
import com.example.entity.SysStaff;
import com.example.entity.SysUser;
import com.example.mapper.contractManage.ContractSigningMapper;
import com.example.service.contractManage.ContractSigningService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ----MaxCV工程师---
 *
 * @author wangbiao
 * @date2024/12/3
 * @desc
 */
@Service
public class ContractSigningServiceImpl implements ContractSigningService {

    @Autowired
    private ContractSigningMapper contractSigningMapper;


    /**
     * 模糊查询,查询全部
     * @param contractVO
     * @return
     */
    @Override
    public List<ContractVO> contractList(ContractVO contractVO) {

        // 先查询表中所有合同信息
        List<ContractVO> list = contractSigningMapper.contractList(contractVO);

        for (ContractVO c:list) {
            // 定义当前时间
            Date time = new Date();
            // 合同开始时间与当前时间对比
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            try {

                // 将合同开始时间和合同结束时间转换为Date类型
                Date contractStartDate = formatter.parse(c.getContractStart());
                Date contractEndDate = formatter.parse(c.getContractEnd());
                if (contractStartDate.after(time)){ // 开始时间在当前时间之后 合同未开始,将合同状态设置为0
                    c.setContractState(0);
                    contractSigningMapper.update(c);
                }else if(!contractEndDate.after(time)){ // 结束时间在当前时间之前,合同已到期.将合同状态设置为2
                    c.setContractState(2);
                    contractSigningMapper.update(c);
                }else { // 合同生效中,将合同状态设置为1
                    c.setContractState(1);
                    contractSigningMapper.update(c);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        // 分页拦截,在哪了需要分页就写在哪里
        PageHelper.startPage(contractVO.getPageNum(),contractVO.getPageSize());

        List<ContractVO> getContractList = contractSigningMapper.contractList(contractVO);
        return getContractList;
    }

    /**
     * 删除单条合同数据
     * @param contractId
     * @return
     */

    @Override
    public int del(int contractId) {
        return contractSigningMapper.del(contractId);
    }
    /**
     * 批量删除
     * @param
     * @return
     */

    @Override
    public int delBatch(ArrayList<Integer> ids) {
        return contractSigningMapper.delBatch(ids);
    }

    /**
     * 添加时根据输入的姓名查询对应的身份证信息
     * @param staffName
     * @return
     */
    @Override
    public SysStaff getCardId(String staffName) {
        return contractSigningMapper.getCardId(staffName);
    }


    /**
     * 添加合同信息
     * @param contractVO
     * @return
     */
    @Override
    public int addContract(ContractVO contractVO) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SysUser user = (SysUser) request.getSession().getAttribute("sysUser");
        String userName = user.getUserName();
        contractVO.setCreateBy(userName);
        contractVO.setCreateTime(new Date());
        // 查询表中的最后一个contractCode
        String findContractCode = contractSigningMapper.findLastContractCode();
        // 将取出的contractCode拆分
        String[] arr = findContractCode.split("C");
        // 取出其中的数字部分
        String lastContractStr = arr[arr.length - 1];
        // 转化为Integer 类型
        Integer lastContract = Integer.parseInt(lastContractStr);
        // 自增1
        lastContract++;
        Integer count = 0;
        Integer temp = lastContract;
        // 计算这个数字是几位数
        while(temp % 10 != 0){
            temp = temp / 10;
            count++;
        }
        String result = "C";
        while (3-count >0){
            result +="0";
            count++;
        }
        result = result+lastContract;
        contractVO.setContractCode(result);
        contractVO.setCheckState(3); // 待提交
        contractVO.setContractState(0);// 未开始
        return contractSigningMapper.addContract(contractVO);
    }

    /**
     * 更新合同信息
     * @param contractVO
     * @return
     */
    @Override
    public int update(ContractVO contractVO) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SysUser user = (SysUser) request.getSession().getAttribute("sysUser");

        String userName = user.getUserName();
        contractVO.setUpdateBy(userName);
        contractVO.setUpdateTime(new Date());
        contractVO.setCheckState(3); // 待提交
        contractVO.setContractState(0);// 未开始
        System.out.println("更新"+contractVO.getCheckState());

        return contractSigningMapper.update(contractVO);
    }


    /**
     * 展示合同类型数量报表
     * @return
     */
    @Override
    public HashMap<String, Object> contractTypeEChars() {
        return contractSigningMapper.contractTypeEChars();
    }


    /**
     * 提交按钮,提交后审核状态更新为待审核
     * @param contractId
     * @return
     */

    @Override
    public int submit(int contractId) {
        return contractSigningMapper.submit(contractId);
    }

    /**
     * 撤销按钮
     * @param contractId
     * @return
     */
    @Override
    public int callBack(int contractId) {
        return contractSigningMapper.callBack(contractId);
    }

    @Override
    public void insertBatch(List<ContractVO> cachedDataList) {
        contractSigningMapper.insertBatch(cachedDataList);
    }


}
